package com.angel.activity;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.angel.activity.LoginActivity.MyIUiListener;
import com.angel.entity.Task;
import com.angel.port.IActivity;
import com.angel.util.BaseUtil;
import com.angel.util.ConstUtil;
import com.angel.util.UserInfoUtil;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class QQBinding extends Activity implements IActivity{

	private Button qq_back_Btn;
	private Button qq_binding;
	private Button cancel_qq_binding;
	private Context context;
	private Tencent mTencent;
	private Intent tent;
	private String isBind;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.qq_binding);
		 MobclickAgent.onError(this);//自动捕获异常退出（FC ）
		tent=getIntent();
		isBind=tent.getStringExtra("isBind");
		context=QQBinding.this;
		MyListener listenr = new MyListener();
		qq_back_Btn = (Button)findViewById(R.id.qq_back_bnt);
		cancel_qq_binding = (Button)findViewById(R.id.cancel_qq_binding);
		qq_binding = (Button)findViewById(R.id.qq_binding);
		qq_back_Btn.setOnClickListener(listenr);
		if(isBind.equals("已绑定")){
			qq_binding.setEnabled(false);
		}else{
			cancel_qq_binding.setEnabled(false);
		}
	}
	
    class MyListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			int id=v.getId();
			if(id==R.id.qq_back_bnt){
				finish();
			}
		}
    }
    /*绑定qq*/
    public void bindQQ(View v){
    	mTencent = Tencent.createInstance(ConstUtil.APP_ID, context.getApplicationContext());
    	mTencent.login(QQBinding.this, ConstUtil.SCOPE, new MyIUiListener());
    }
    /*解除qq绑定*/
    public void unBindQQ(View v){
    	UserInfoUtil.clearQQInfo(context);
    	finish();//
    }
    /*qq登录回调类*/
    class MyIUiListener implements  IUiListener {
		@Override
		public void onCancel() {
		}
		@Override
		public void onComplete(JSONObject arg0) {
			try {
				String openid = arg0.getString("openid");
				String access_token=arg0.getString("access_token");
				String expires_in=arg0.getString("expires_in");
				long expiresIn= System.currentTimeMillis() + Long.parseLong(expires_in) * 1000;
				SharedPreferences.Editor editor=context.getSharedPreferences(UserInfoUtil.USER_TABLE,Context. MODE_WORLD_WRITEABLE).edit();
				editor.putString("openid", openid);
				editor.putString("qq_access_token", access_token);
				editor.putLong("qq_expires_in",expiresIn);
				editor.commit();
				mTencent.setAccessToken(access_token, expiresIn+"");
				bindQQ();//绑定qq
				finish();
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		@Override
		public void onError(UiError arg0) {
			BaseUtil.makeToast(QQBinding.this, "error code:"+arg0.errorCode);
		}
	}
    public void bindQQ(){
    	HashMap<String,String> map=new HashMap<String,String>();
		map.put("userId", UserInfoUtil.getValue(context, "userId", ""));
		map.put("qq",  UserInfoUtil.getValue(context, "openid", ""));
    	BaseUtil.startService(Task.QQBIND,this,map);//启动登陆
    }
	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void refresh(Object... params) {
		// TODO Auto-generated method stub
	}
	@Override
	protected void onResume() {
		super.onResume();
		 MobclickAgent.onResume(this);
	}
	@Override
	protected void onPause() {
		super.onPause();
		 MobclickAgent.onPause(this);
	}
}
